package com.cjm.one.custom.apps.controller;

import com.cjm.one.common.constant.RedisConstants;
import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.custom.service.CustomerInfoService;
import com.cjm.one.model.base.TokenVo;
import com.cjm.one.model.custom.vo.RegisterVO;
import com.cjm.one.model.system.vo.LoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Email;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Tag(name = "客户登录管理")
@RequestMapping("/app/customer")
public class AppResgisterController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private CustomerInfoService customerInfoService;

    @PostMapping("/register")
    @Operation(summary = "客户注册")
    public Result<Boolean> register(@Validated @RequestBody RegisterVO registerVO)  {
        String storageSMSCode = (String)redisTemplate.opsForValue().get(RedisConstants.CAPTCHA_SMSCODE_KEY_EXPIRE + registerVO.getEmail());
        if(storageSMSCode == null){
            throw new GlobalException(ResultCodeEnum.EMAIL_CODE_EXPIRED);
        }
        log.info("存储的验证码为storageSMSCode:{},getSmsCode:{},flag:{}",storageSMSCode,registerVO.getSmsCode(),storageSMSCode.equals(registerVO.getSmsCode()));
        if (!storageSMSCode.equals(registerVO.getSmsCode())) {
            throw new GlobalException(ResultCodeEnum.EMAIL_CODE_MISMATCH);
        }
        return Result.ok(customerInfoService.register(registerVO));

    }
    @GetMapping("checkEmail")
    @Operation(summary = "检查邮箱是否被注册")
    public Result<Boolean> checkEmail(@Email @RequestParam String email){
        return Result.ok(customerInfoService.getByEmail(email) == null);
    }



}
